package im.vinci.server.headphone.service;

import im.vinci.server.common.exceptions.VinciException;
import im.vinci.server.common.exceptions.error.ErrorCode;
import im.vinci.server.headphone.domain.*;
import im.vinci.server.headphone.persistence.VisualDataMapper;
import im.vinci.server.utils.BizTemplate;
import org.elasticsearch.common.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by mingjie on 17/3/6.
 */
@Service
public class VisualService {

    @Autowired
    private VisualDataMapper visualDataMapper;

    public List<VisualCategory> getVisualData(LanguageType languageType, String deviceId) {
        return new BizTemplate<List<VisualCategory>>(getClass().getSimpleName() + "getVisualData") {
            @Override
            protected void checkParams() throws VinciException {
                if (languageType == null) {
                    throw new VinciException(ErrorCode.ARGUMENT_ERROR, "不支持的语言类型", "");
                }
            }

            @Override
            protected List<VisualCategory> process() throws Exception {
                List<VisualData> list = visualDataMapper.select(languageType.getCode());
                if (!Strings.isNullOrEmpty(deviceId)) {
                    List<VisualBrandCategory> brandCategories = visualDataMapper.selectBrandCategory(deviceId);

                    if (!brandCategories.isEmpty()) {
                        Set<String> categorySet = new HashSet<String>();
                        for (VisualBrandCategory brandCategory : brandCategories) {
                            categorySet.add(brandCategory.getCategory());
                        }

                        List<VisualData> brandData = visualDataMapper.selectBrandData(categorySet);
                        if (!brandData.isEmpty()) {
                            list.addAll(brandData);
                        }
                    }
                }

                List<VisualCategory> resultList = new ArrayList<>();

                if(!list.isEmpty()) {
                    HashMap<String, List<VisualData>> dataMap = new HashMap<String, List<VisualData>>();
                    for (VisualData visualData : list) {
                        List<VisualData> dataList = dataMap.get(visualData.getCategory());
                        if (dataList == null) {
                            dataList = new ArrayList<VisualData>();
                            dataMap.put(visualData.getCategory(), dataList);
                        }
                        dataList.add(visualData);
                    }

                    int categoryId = 1;
                    for (String categoryName : dataMap.keySet()) {
                        VisualCategory visualCategory = new VisualCategory();
                        visualCategory.setCategoryId(categoryId);
                        visualCategory.setCategory(categoryName);

                        List<VisualData> tempList = dataMap.get(categoryName);
                        if (tempList != null && !list.isEmpty()) {
                            Collections.sort(tempList, new Comparator<VisualData>() {
                                @Override
                                public int compare(VisualData o1, VisualData o2) {
                                    if (o1.getPriority() > o2.getPriority()) {
                                        return -1;
                                    } else if (o1.getPriority() == o2.getPriority()) {
                                        return 0;
                                    } else {
                                        return 1;
                                    }
                                }
                            });
                        }
                        visualCategory.setData(tempList);

                        categoryId++;

                        resultList.add(visualCategory);
                    }
                }

                return resultList;
            }
        }.execute();
    }

    public List<BarrageText> getBarrageData(LanguageType languageType) {
        return new BizTemplate<List<BarrageText>>(getClass().getSimpleName() + "getBarrageData") {
            @Override
            protected void checkParams() throws VinciException {
                if (languageType == null) {
                    throw new VinciException(ErrorCode.ARGUMENT_ERROR, "不支持的语言类型", "");
                }
            }

            @Override
            protected List<BarrageText> process() throws Exception {
                List<BarrageText> list = visualDataMapper.selectBarrageText(languageType.getCode());

                return list;
            }
        }.execute();
    }
}
